package work;

import book.Book;
import book.Booklist;

import java.util.Scanner;

public class Input {
    public static int input(Booklist booklist) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入图书全名或者图书ID");
        String input = scanner.next();
        int size = booklist.getBooklistsize();
        if(input != null && input.matches("^[0-9]+$")){//判断输入的字符串是否为整形，如果是整数，则该输入的目标是图书ID
            if (Integer.parseInt(input) <= Book.getId() && Integer.parseInt(input) > 0) {
                for (int i = 0; i < size; i++) {
                    if (booklist.getBook(i).getBookid() == Integer.parseInt(input)) {
                        return i;
                    }
                }
                System.out.println("未找到该图书");
                return -1;
            }else{
                System.out.println("输入ID超出书架图书范围！");
                return -1;
            }
        }else{
            for (int i = 0; i < size; i++) {
                if (booklist.getBook(i).getName().equals(input)) {
                    return i;
                }
            }
            System.out.println("未找到该图书");
            return -1;
        }
    }
}
